package io.renren.modules.substation.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.substation.entity.TInfoUserAreaEntity;
import io.renren.modules.substation.service.TInfoUserAreaService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 用户区域权限表
 *
 * @author lan
 * @email sunlightcs@gmail.com
 * @date 2020-06-20 17:05:11
 */
@RestController
@RequestMapping("substation/tinfouserarea")
public class TInfoUserAreaController extends AbstractController {
    @Autowired
    private TInfoUserAreaService tInfoUserAreaService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tinfouserarea:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tInfoUserAreaService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("substation:tinfouserarea:info")
    public R info(@PathVariable("id") Long id){
		TInfoUserAreaEntity tInfoUserArea = tInfoUserAreaService.getById(id);

        return R.ok().put("tInfoUserArea", tInfoUserArea);
    }

    /**
     * 信息
     */
    @RequestMapping("/userAreainfo/{id}")
    @RequiresPermissions("substation:tinfouserarea:userAreainfo")
    public R userAreainfo(@PathVariable("id") Long id){
        SysUserEntity user = sysUserService.getById(id);

        //获取用户所属的区域列表
        List<Long> areaIdList = new ArrayList<>();
        List<TInfoUserAreaEntity> list = tInfoUserAreaService.listByUserId(id);
        for (TInfoUserAreaEntity o : list) {
            if (o.getAreaType().equals("0"))//区域
                areaIdList.add(o.getUserArea());
             else//设备
                areaIdList.add(0L - o.getUserArea());

        }

        user.setAreaIdList(areaIdList);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tinfouserarea:save")
    public R save(@RequestBody TInfoUserAreaEntity tInfoUserArea){
		tInfoUserAreaService.save(tInfoUserArea);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tinfouserarea:update")
    public R update(@RequestBody TInfoUserAreaEntity tInfoUserArea){
		tInfoUserAreaService.updateById(tInfoUserArea);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tinfouserarea:delete")
    public R delete(@RequestBody Long[] ids){
		tInfoUserAreaService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 配置用户观看区域
     */
    @RequestMapping("/config")
    @RequiresPermissions("substation:tinfouserarea:config")
    public R config(@RequestBody SysUserEntity sysUserEntity){
        tInfoUserAreaService.saveUserArea(sysUserEntity.getUserId(), sysUserEntity.getAreaIdList(), getUserId());
        return R.ok();
    }

}
